// @require jquery-1.11.0.min.js
(function ($) {

  $.ScrollBookReader = function (options) {

    var defaults = {
      title_selector: "",
      content_selector: "",
      background_selector: "",
      next_page_selector: "",
      prev_page_selector: "",
      // --- css
      title_css: {
        "font": "微软雅黑",
        "font-size": "64px",
        "font-weight": "bold",
        "color": "black",
        "line-height": "2em"
      },
      content_css: {
        "font-family": "华文中宋",
        "font-size": "36px",
        "font-weight": "bold",
        "color": "black",
        "word-wrap": "break-word",
        "line-height": "3em",
        "padding-left": "345px",
        "padding-right": "345px",
        "height": "540px"
      },
      background_css: {
        "background-color": "rgb(127, 168, 127)",
        "width": "100%"
      },
      remove: [],
      ads: [
        /<div .*?<\/div>/ig, /<a .*?<\/a>/ig, /http:\/\/.*?.html/ig, /<\/?p>/ig, /&nbsp;/ig, /\n/ig, /<br>/ig,
        /[１1][０0][８8][小尒][说説][Ww]{3}[\.。][BboOkK108ｂｏｋＢＯＫ１０８]{7}([\.。][cＣC][oＯO][mＭM])?[更鯁][噺|新]/ig
      ],
      must_break: {
        "”": 1,
        "“": 0,
        "？": 1,
        "！": 1,
        "。": 1,
        "；": 1,
        "《": 0,
        "》": 1
      },
      may_break: {
        "，": 1,
        "、": 1
      }
    };
    alert(121)

    var opts = $.extend(defaults, options);

    var next = 0;
    var text_array = [];
    var index = 0;


    this.txt_content = $(opts.content_selector).html();

    $(opts.content_selector).html("");
    if (!txt_content) return;

    console.log("loaded...");
    // css
    $(opts.title_selector).css(opts.title_css);
    $(opts.content_selector).css(opts.content_css);
    $(opts.background_selector).css(opts.background_css);
    // more css


    // 去除的内容

    $.each(opts.remove, function (i, n) {
      $(n).remove();
    });

    var txt_content = this.txt_content;
    // 去除广告
    $.each(opts.ads, function (i, n) {
      txt_content = txt_content.replace(n, "");
    });

    // --------------------------------------------------------------------------------------
    var length = 26;
    var total = this.txt_content.length;
    var i = 0;

    for (index = 0; index < total;) {
      var line = txt_content.substr(index, length + 4); // 向后宽松字符
      var tmp = "";
      var indexOfBreak;

      // must_break
      var find_break = false;


      $.each(opts.must_break, function (idx, item) {
        if (find_break) return;

        indexOfBreak = line.lastIndexOf(idx);

        if (indexOfBreak != -1 && indexOfBreak != item) {

          tmp = txt_content.substr(index, indexOfBreak + item);

          if (tmp !== undefined) {
            tmp = $.trim(tmp);
            if (tmp !== "") {
              text_array[i++] = tmp;
            }
            index += ( indexOfBreak + item );
            find_break = true;
          }
        }
      });
      if (find_break) continue;

      $.each(opts.may_break, function (idx, item) {
        if (find_break) return;

        indexOfBreak = line.lastIndexOf(idx);

        if (indexOfBreak != -1 && indexOfBreak >= length - 6 && indexOfBreak != item) {

          tmp = txt_content.substr(index, indexOfBreak + item);

          if (tmp !== undefined) {
            tmp = $.trim(tmp);
            if (tmp !== "") {
              text_array[i++] = tmp;
            }
            index += ( indexOfBreak + item );
            find_break = true;
          }
        }
      });

      if (find_break) continue;

      tmp = txt_content.substr(index, length);
      if (tmp !== undefined) {
        tmp = $.trim(tmp);
        if (tmp !== "") {
          text_array[i++] = tmp;
        }
        index += length;
      }
    }

    // ---------------------


    var $list = $("<div>");
    $(opts.content_selector).html($list);
    $list.append("<li class='item' style='color:black;list-style-type;none'>");
    $list.append("<li class='item' style='color:black;list-style-type;none'>");
    $list.append("<li class='item' style='color:black;list-style-type;none'>");
    showContent();
    // 翻页
    $(opts.next_page_selector).click(function () {
      document.location = $(this).attr("href");
    });

    $(document).keydown(function (event) {
      var direction = "";
      switch (event.keyCode) {
        // Space
        case 32 :
          direction = "down";
          break;
        // Right Arrow
        case 39 :
          direction = "down";
          break;
        // Dw Arrow
        case 40 :
          direction = "down";
          break;
        // Up Arrow
        case 37 :
          direction = "up";
          break;
        // Left Arrow
        case 38 :
          direction = "up";
          break;
        default:
          return;
      }


      if (next >= text_array.length) {
        $(opts.next_page_selector).trigger("click");
      } else {
        showContent(direction);
      }
    });
    function showContent(direction) {
      var prev_content = "";
      var curt_content = "";
      var next_content = "";


      var light;
      var gray;
      var move = false;
      var first;
      var last;
      var init = false; // 第一次运行
      var up = direction == "up";

      if (up) {
        prev_content = text_array[next - 3] || "";
        curt_content = text_array[next - 2] || "";
        next_content = text_array[next - 1] || "";
        next--;
      } else {
        prev_content = text_array[next - 1] || "";
        curt_content = text_array[next] || "";
        next_content = text_array[next + 1] || "";
        next++;

      }
      console.log(direction);

      if ($list.find("li.item:nth-child(1)").html() === "") {
        $list.find("li.item:nth-child(1)").html(prev_content || "----");
        $list.find("li.item:nth-child(2)").html(curt_content || "----");
        $list.find("li.item:nth-child(3)").html(next_content || "----");
        init = true;
      } else {
        init = false;
        move = true;
        if (up) {
          $list.prepend("<li class='item' style='display:none;color:black;opacity: 0;list-style-type;none'>" + prev_content || "----" + "</li>");
        } else {
          $list.append("<li class='item' style='display:none;color:black;opacity: 0;list-style-type;none'>" + next_content || "----" + "</li>");
        }
      }

      first = $list.find("li.item:first");
      last = $list.find("li.item:last");

      if (up) {
        light = $list.find("li.item:odd").not(":last");
        gray = $list.find("li.item:even");
      } else if (init) {
        light = $list.find("li.item:odd");
        gray = $list.find("li.item:even");
      } else {
        light = $list.find("li.item:even").not(":first");
        gray = $list.find("li.item:odd");
      }

      $("li").css({
        "list-style-type": "none"
      });

      light.animate({opacity: 1}, {queue: true, duration: 200});
      gray.animate({opacity: 0.1}, {queue: true, duration: 200});

      if (move) {
        if (up) {
          last.animate({"height": 0, opacity: 0}, {queue: false, duration: 200}).remove();
          first.animate({"display": 1, "height": "100%", opacity: 0.1}, {queue: false, duration: 200});

        } else {
          first.animate({"height": 0, opacity: 0}, {queue: false, duration: 200}).remove();
          last.animate({"display": 1, "height": "100%", opacity: 0.1}, {queue: false, duration: 200});
        }
      }
    }
  };


})(jQuery);